/*
    交换机相关类的测试
*/

#include "../../src/server/exchanger.hpp"
#include "../../src/common/logger.hpp"
#include "gtest/gtest.h"


mq::ExchangerManager::ExchangerManagerPtr exchanger_manager_ptr;
class ExchangerTest: public testing::Environment
{
public:
    virtual void SetUp() override
    {
        exchanger_manager_ptr = std::make_shared<mq::ExchangerManager>("./data/mate.db");
    }

    virtual void TearDown() override
    {
        exchanger_manager_ptr->clear();
    }
};


// 插入测试 & 回复数据测试    
TEST(exchanger_test, insert_test)
{
    google::protobuf::Map<std::string, std::string> args;
    args.insert(google::protobuf::MapPair<std::string, std::string>("key1", "value1"));
    args.insert(google::protobuf::MapPair<std::string, std::string>("key2", "value2"));
    exchanger_manager_ptr->declareExchanger("exchanger_1", mq::ExchangerType::DIRECT, true, false, args);
    exchanger_manager_ptr->declareExchanger("exchanger_2", mq::ExchangerType::FANOUT, true, false, args);
    exchanger_manager_ptr->declareExchanger("exchanger_3", mq::ExchangerType::TOPIC, true, false, args);
    exchanger_manager_ptr->declareExchanger("exchanger_4", mq::ExchangerType::DIRECT, true, false, args);
    ASSERT_EQ(exchanger_manager_ptr->size(), 4);

    // 重复插入
    bool ret = exchanger_manager_ptr->declareExchanger("exchanger_2", mq::ExchangerType::DIRECT, true, false, args);
    ASSERT_EQ(ret, true);
    ASSERT_EQ(exchanger_manager_ptr->size(), 4);
}

// 查找测试
TEST(exchanger_test, get_test)
{
    ASSERT_EQ(exchanger_manager_ptr->hasExchanger("exchanger_1"), true);
    ASSERT_EQ(exchanger_manager_ptr->hasExchanger("exchanger_2"), true);
    ASSERT_EQ(exchanger_manager_ptr->hasExchanger("exchanger_3"), true);
    ASSERT_EQ(exchanger_manager_ptr->hasExchanger("exchanger_4"), true);

    mq::Exchanger::ExchangerPtr exchanger_ptr = exchanger_manager_ptr->getExchanger("exchanger_1");
    ASSERT_EQ(exchanger_ptr->_name, "exchanger_1");
    ASSERT_EQ(exchanger_ptr->_type, mq::ExchangerType::DIRECT);
    ASSERT_EQ(exchanger_ptr->_durable, true);
    ASSERT_EQ(exchanger_ptr->_auto_delete, false);
    ASSERT_EQ(exchanger_ptr->getArgs(), "key1=value1&key2=value2");
}

// 删除测试
TEST(exchanger_test, delete_test)
{
    exchanger_manager_ptr->removeExchanger("exchanger_2");
    ASSERT_EQ(exchanger_manager_ptr->size(), 3);
    ASSERT_EQ(exchanger_manager_ptr->hasExchanger("exchanger_2"), false);
}

int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new ExchangerTest);
    return RUN_ALL_TESTS();
}
